package com.java.huxb.rabbitmq.demo.demoexpire.basedemo;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author huxb
 * @date 2020/2/1 0001 下午 2:59
 */
public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        // 创建连接工厂, 初始化配置
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("119.27.175.48");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");
        // 通过链接工程获取链接
        Connection connection = connectionFactory.newConnection();
        // 通过connection 获取Channel
        Channel channel =  connection.createChannel();
        //  声明队列 Queue
        String queueName = "test001";
        channel.queueDeclare(queueName, true, false, false, null);
        //设置消费者
        QueueingConsumer queueingConsumer = new QueueingConsumer(channel);
        // 设置Channel
        channel.basicConsume(queueName, true, queueingConsumer);

        while (true) {
          QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery();
            String s = new String(delivery.getBody());
            System.err.println("消费端： " + s);

        }
    }
}
